#计时功能版本3，采用闭包方式实现
#功能：运行某个函数，如：print_odd()或wait_while()，并记录函数运行的时间
import time

def print_odd(n):
    for i in range(n):
        if i%2==0:
            print(i,end=",")
    print("\n print_odd is over...")

def wait_while(n1,n2,n3):
    time.sleep(n1+n2+n3)
    print("\nwait_while is over...")

#count_time函数形参func是一个函数
#count_time函数内嵌套定义了wrapper函数，并返回了wrapper
# wrapper函数功能：计算func函数（count_time形参）的运行时长
def count_time(func):
    def wrapper(*args,**kwargs):
        start = time.time()
        ret=func(*args, **kwargs)
        end = time.time()
        print("it takes {}s".format(end - start))
        return ret

    return wrapper

print_odd_decorated=count_time(print_odd)
print_odd_decorated(1000)

wait_while=count_time(wait_while)
wait_while(0.5,0.4,0.2)